package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.entity.PageParam;
import com.itheima.reggie.service.EmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/employee")
@Api("员工信息接口")
public class EmployeeController {

    //注入service
    @Autowired
    private EmployeeService es;

    /**
     * 更新员工
     *
     * @param employee
     * @param session
     * @return
     */
    @PutMapping
    @ApiOperation("更新员工信息")
    public R update(@RequestBody Employee employee, HttpSession session) {
        Employee employee1 = (Employee) session.getAttribute("employee");
        int row = es.update(employee, employee1.getId());
        if (row > 0) {
            return R.success("更新状态成功");
        } else {
            return R.error("更新状态失败");
        }
    }

    /**
     * 分页查询
     *
     * @param pageParam
     * @return
     */
    @GetMapping("/page")
    public R<IPage<Employee>> findPage(PageParam pageParam) {
        System.out.println(pageParam);
        try {
            IPage<Employee> page = es.findPage(pageParam);
            return R.success(page);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("分页查询失败");
        }

    }


    /**
     * 添加员工
     *
     * @param employee
     * @param session
     * @return
     */
    @PostMapping
    public R add(@RequestBody Employee employee, HttpSession session) {
        Employee employee1 = (Employee) session.getAttribute("employee");
        int row = es.add(employee, employee1.getId());
        if (row > 0) {
            return R.success("添加成功");
        } else {
            return R.error("添加失败");
        }


    }

    // * 退出登录
//     *     1. 在服务器端要把曾经登录成功之后保存的员工对象给删除了。
//            *     2. 还可以更进一步，就是把session都销毁了。
    @PostMapping("/logout")
    public R<String> logout(HttpSession session) {
        try {
            //1. 清空session即可
            //session.removeAttribute("employee");

            //让session失效，把session的数据全部干掉，并且回收这个内存。
            session.invalidate();

            return R.success("退出成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("退出失败");
        }
    }

    /**
     * 登录
     * 添加员工
     * @param employee
     * @param session
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee , HttpSession session){

        //1. 交代service干活
        Employee loginEmployee = es.login(employee);

        //2. 判断结果：
        if(loginEmployee == null){ //登录失败！
            return R.error("用户名或者密码错误！");
        }else { //登录成功！

            //1. 先把员工对象保存到session作用域
            session.setAttribute("employee", loginEmployee);

            //2. 返回信息给页面
            return R.success(loginEmployee);
        }
    }
}